<template>
  <div class="MyOrder" style="width: 90%; margin-left: 80px">
    <ul class="tagList">
      <li
        :class="{ selected: curIndex === index }"
        v-for="(item, index) in tagList"
        :key="'tag' + index"
        @click="changeIndex(index)"
      >
        {{ item }}
      </li>
    </ul>
    <div class="orderBox">
      <div class="orderTableHeader">
        <span class="order-table__image"></span>
        <span class="order-table__name">商品</span>
        <span class="order-table__price">单价</span>
        <span class="order-table__count">数量</span>
        <span class="order-table__real-price">实付款</span>
        <span class="order-table__action">交易操作</span>
      </div>
      <ul class="orderList">
        <li v-for="item in orderList" :key="'order' + item.id">
          <div class="orderHeader">
            <span class="orderId orderTime">{{ '订单号：' + item.id }}</span>
            <span class="state orderTime">
              订单状态：{{ item.status | orderStatusFilter }}
            </span>
            <span class="orderTime"> 订单创建时间：{{ item.createTime}}</span>
            <!--<span class="deleteBtn" @click="deleteOrder(item.id)"><i class="iconfont icon-close" /></span>-->
          </div>
          <ul>
            <li v-for="product in item.products" :key="'product' + product.id">
              <div class="orderDetail">
                <img
                  class="order-table__image"
                  :src="product.productImgUrl"
                  alt="商品图片"
                />
                <div class="order-table__name">
                  <!--<p @click="navTo('/mall/goods/'+product.id)">{{product.productName}}</p>-->
                  {{ product.productName }}
                </div>
                <span class="order-table__price">
                  ￥{{ product.price | rounding }}
                </span>
                <span class="order-table__count">{{ product.count }}</span>
                <span class="order-table__real-price">
                  ￥{{ product.productTotalAmount | rounding }}
                </span>

                <!--<span class="hasComment" v-else-if="item.state===3 && item.hasComment">已评价</span>-->
              </div>
            </li>
          </ul>
          <span
            class="order-table__real-price"
            style="margin-left: 1100px; margin-top: 10px"
          >
            <el-button
              type="warning"
              @click="modifyAddress(item.id, item.userAddressId)"
            >
              修改地址
            </el-button>
            <el-button
              type="primary"
              v-if="item.status === 1"
              @click="confirmPay(item.id)"
            >
              确认付款
            </el-button>
            <el-button
                type="primary"
                v-else-if="item.status === 2"
                @click="confirmPay(item.id)"
            >
              待发货
            </el-button>
            <el-button
              type="primary"
              v-else-if="item.status === 3"
              @click="confirmReceive(item.id)"
            >
              确认收货
            </el-button>
          </span>
          <!--<el-divider></el-divider>-->
        </li>
      </ul>
    </div>
    <div v-if="orderList.length === 0" style="text-align: center; ">
      <br/><br/><br/><br/>
      <h2>暂无数据</h2>
      <br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
    </div>
    <!--修改订单地对话框-->
    <el-dialog title="修改订单地址" :visible.sync="dialogVisible" width="50%">
      <el-radio-group v-model="addressId">
        <div class="selectItem" v-for="(item, index) in addresses" :key="index">
          <el-radio :label="item.id" @change="changeAddress">
            <span> 收货人：{{ item.consignee }} </span>
            <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>
            <span> 电话：{{ item.mobile }} </span>
            <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>
            <span> 详细地址： {{ item.address }}</span>
          </el-radio>
        </div>
      </el-radio-group>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="updateAddress">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { findOrder, updateOrder } from '../../api/user/order';
import {getToken, getUser} from '../../utils/baseinfo';
import { findAddress } from '../../api/user';

const statusMap = {
  1: '待付款',
  2: '待发货',
  3: '待收货(已发货)',
  5: '成功',
  6: '失败',
  7: '退货退款',
};
export default {
  name: 'Order',

  data() {
    return {
      tagList: ['全部订单', '待付款', '待发货', '已发货'],
      curIndex: 0,
      orderList: [],
      popupShow: false,
      curOrderId: '',
      curCommentGoodsId: '',
      curCommentGoodsDetailId: '',
      curStar: 0,
      hasClickStar: false,
      comment: '',

      page: 1,
      size: 10,
      addresses: [],
      addressId: 0,
      dialogVisible: false,
    };
  },

  created() {
    this.checkLogin()
    this.getOrderList();
  },

  filters: {
    orderStatusFilter(status) {
      return statusMap[status];
    },
    rounding(value) {
      return value.toFixed(2);
    },
  },

  methods: {
    checkLogin() {
      const token = getToken();
      if(token == undefined || token == '') {
        this.$message.error("未登录，请登录")
        this.$router.push({path: '/'})
      }
    },
    getOrderList(status) {

      const userId = getUser() != null ? getUser().id : '';
      findOrder(userId, status, this.page, this.size)
        .then((response) => {
          this.orderList = response.data.orders;

          console.log('orders');
          console.log(this.orderList);
        })
        .catch((error) => {
          console.log(error);
        });
    },

    changeAddress() {
      // console.log("addressID")
      // console.log(this.addressId)
    },

    changeIndex(i) {
      let status = null;
      this.curIndex = i;

      if (i != 0) {
        let name = this.tagList[i];
        if (name == '已发货') {
          status = 3;
        } else {
          for (let key in statusMap) {
            let value = statusMap[key];
            if (value == name) {
              status = key;
            }
          }
        }
      }
      console.log('status');
      console.log(status);

      this.getOrderList(status);
    },

    modifyAddress(orderId, userAddressId) {
      this.curOrderId = orderId;
      this.addressId = userAddressId;
      this.getAddress();
      this.dialogVisible = true;
    },

    updateAddress() {
      console.log('orderID');
      console.log(this.curOrderId);
      console.log('addressID');
      console.log(this.addressId);
      updateOrder(this.curOrderId, this.addressId, null)
        .then((response) => {
          this.dialogVisible = false;
          this.$message.success('修改地址成功');
          this.getOrderList(null);
        })
        .catch((error) => {
          console.log(error);
        });
    },

    getAddress() {
      const userId = getUser() != null ? getUser().id : '';

      findAddress(userId, 1, 100)
        .then((response) => {
          this.addresses = response.data.addresses;
        })
        .catch((error) => {
          console.log(error);
        });
    },

    navTo(route) {
      // this.$router.push(route);
    },

    confirmPay(orderId) {
      // 做支付
    },
    confirmReceive(orderId) {
      updateOrder(orderId, null, 5)
        .then((response) => {
          this.$message.success('确认收货成功');
        })
        .catch((error) => {
          console.log(error);
        });
    },
  },
};
</script>

<style scoped lang="less">
@import '../../assets/css/var.less';
.MyOrder {
  .tagList {
    li {
      text-align: center;
      display: inline-block;
      font-weight: 550;
      font-size: 18px;
      border-bottom: 2px solid @borderColor;
      cursor: pointer;
      padding: 10px 20px;
      position: relative;
      &:after {
        top: 12px;
        right: 0;
        position: absolute;
        content: '';
        width: 1px;
        height: 15px;
        background-color: @borderColor;
      }
      &:last-child {
        &:after {
          display: none;
        }
      }
    }
    .selected {
      color: #ff3030;
      border-bottom: 2px solid #ff3030;
    }
  }
  .orderBox {
    .order-table {
      &__image {
        width: 120px !important;
        height: 120px !important;
      }
      &__name {
        width: 300px !important;
      }
      &__title {
        width: 200px !important;
      }
      &__price {
        width: 200px !important;
      }
      &__real-price {
        width: 200px !important;
      }
      &__count {
        width: 200px !important;
      }
    }

    width: 100%;
    margin-top: 20px;
    .orderTableHeader {
      background-color: #f5f5f5;
      border: 1px solid @borderColor;
      color: @fontDefaultColor;
      font-size: 14px;
      padding: 10px;
      span {
        display: inline-block;
        &:first-child {
          text-align: center;
        }
      }
    }

    .orderList {
      width: 100%;
      li {
        border: 1px solid @borderColor;
        font-size: 13px;
        margin-top: 10px;
        .orderHeader {
          background-color: #f1f1f1;
          height: 40px;
          line-height: 40px;
          padding: 0 5px;
          .orderTime {
            font-weight: 600;
            margin-right: 5rem;
          }
          .orderId,
          .state {
            margin-left: 10px;
          }
          .deleteBtn {
            float: right;
            cursor: pointer;
            i {
            }
          }
        }
        .orderDetail {
          display: flex;
          align-items: center;
          width: 100%;
          padding: 10px;
          position: relative;
          overflow: hidden;
          img {
            width: 84px;
            height: 84px;
            display: inline-block;
          }
          .goodsName {
            display: inline-block;
            align-items: center;
            width: 230px;
            p {
              cursor: pointer;
              line-height: 20px;
              &:hover {
                text-decoration: underline;
              }
            }
            span {
              color: @fontDefaultColor;
              display: block;
              margin-top: 10px;
            }
          }
          .unitPrice,
          .num,
          .amount,
          .hasComment {
            display: inline-block;
            vertical-align: top;
            width: 15%;
            height: 85px;
            line-height: 85px;
          }
          button {
            //position: absolute;
            //right: 90px;
            //bottom: 40px;
            //width: 70px;
            //height: 30px;
            //border-radius: 3px;
            ////background-color: @thirdColor;
            //color:white;
            //border: none;
          }
        }
      }
    }
  }
  .popupContent {
    padding: 10px;
    .scoreBox {
      width: 100%;
      height: 50px;
      position: relative;
      line-height: 50px;
      text-align: left;
      .tips {
        font-size: 15px;
        vertical-align: middle;
        display: inline-block;
      }
      i {
        cursor: pointer;
        vertical-align: middle;
        display: inline-block;
        font-size: 25px;
        margin-right: 5px;
        -webkit-text-stroke: 1px #f9bd4f;
      }
    }
    textarea {
      width: 400px;
      height: 80px;
      padding: 5px;
      border: 1px solid @borderColor;
      display: block;
      margin-top: 10px;
    }
    button {
      display: block;
      margin: 10px auto;
      width: 70px;
      height: 30px;
      border-radius: 3px;
      background-color: #313541;
      color: white;
      border: none;
    }
  }
}
</style>
